package com.explorati.LeetCode003.lengthOfLongestSubstring;

public class Solution2 {
	public int lengthOfLongestSubstring(String s) {
		int n = s.length();
		int[] freq = new int[256];
		int l = 0, r = -1;
		int res = s.length() + 1;
		while (l < n) {
			if (r + 1 < s.length() && freq[s.charAt(r + 1)] == 0) {
				r++;
				freq[s.charAt(r)]++;
			} else {
				freq[s.charAt(l)]--;
				l++;
			}
			res = Math.max(res, r - l + 1);
		}
		return res;
	}
}
